﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Objects;

namespace medder
{
    public partial class AssignPat : Form
    {
        Patient patient;
        MedBaseEntities med = new MedBaseEntities();
        public static ObjectSet<Profile> profiles;
        public static ObjectSet<Region> regions;
        public static ObjectSet<Doctor> doctors;
        public static ObjectSet<TimeTable> timetables;
        public static ObjectSet<User> users;
        public static List<Doctor> usedDoctors = new List<Doctor>();

        public AssignPat(Patient patient)
        {
            this.patient = patient;
            InitializeComponent();
            profiles = med.Profiles;
            regions = med.Regions;
            doctors = med.Doctors;
            timetables = med.TimeTables;
            users = med.Users;
        }

        private void AssignPat_Load(object sender, EventArgs e)
        {

            bookView.Columns.Add("Number", "№");
            bookView.Columns["Number"].ReadOnly = true;
            bookView.Columns["Number"].Width = 30;

            bookView.Columns.Add("Doctor", "Врач");
            bookView.Columns["Doctor"].ReadOnly = true;
            bookView.Columns["Doctor"].Width = 235;

            bookView.Columns.Add("Date", "Дата");
            bookView.Columns["Date"].ReadOnly = true;
            bookView.Columns["Date"].Width = 80;

            bookView.Columns.Add("Time", "Время");
            bookView.Columns["Time"].ReadOnly = true;
            bookView.Columns["Time"].Width = 80;


            foreach (var x in profiles)
            {
                comboBox1.Items.Add(x.ProfName);
            }

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!comboBox1.Text.Equals(String.Empty))
            {
                comboBox2.Items.Clear();
                string profile = comboBox1.SelectedItem.ToString();
                int profId = profiles.Where(x => x.ProfName.Equals(profile)).FirstOrDefault().ProfId;
                usedDoctors = new List<Doctor>();
                foreach (var doc in doctors)
                {

                    if (doc.ProfId == profId)
                    {
                        if ((doc.Region != null) && (doc.Region == patient.Region))
                        {
                            User docUser = users.Where(x => x.Passport.Equals(doc.Passport)).FirstOrDefault();
                            usedDoctors.Add(doc);
                            comboBox2.Items.Add(string.Format("{0} {1} {2}", docUser.Surname, docUser.Name, docUser.Patronymic));
                        }
                    }
                }
                List<TimeTable> timeTable = GetTable(usedDoctors, patient);
                FillTable(timeTable);
            }
            comboBox2.Enabled = true;
        }

        private List<TimeTable> GetTable(List<Doctor> usedDoctors, Patient patient)
        {
            List<TimeTable> timetable = new List<TimeTable>();
            foreach (Doctor doc in usedDoctors)
            {
                timetable.AddRange(timetables.Where(x => x.DoctorID.Equals(doc.Passport)).AsEnumerable<TimeTable>());
            }
            return timetable;

        }

        private void FillTable(List<TimeTable> timeTable)
        {
            bookView.Rows.Clear();
            int i = 0;
            foreach (TimeTable table in timeTable)
            {
                User docUser = users.Where(x => x.Passport.Equals(table.DoctorID)).FirstOrDefault();
                bookView.Rows.Add();
                bookView.Rows[i].Cells[0].Value = table.ID;
                bookView.Rows[i].Cells[1].Value = string.Format("{0} {1} {2}", docUser.Surname, docUser.Name, docUser.Patronymic);
                bookView.Rows[i].Cells[2].Value = Convert.ToDateTime(table.Date).ToShortDateString();
                bookView.Rows[i].Cells[3].Value = Convert.ToDateTime(table.Time).ToShortTimeString();
                i++;
            }
            
        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!comboBox2.Text.Equals(String.Empty))
            {
                int docId = comboBox2.SelectedIndex;
                Doctor doc = usedDoctors.ElementAt<Doctor>(docId);
                List <Doctor> savedDoctors = new List<Doctor>();
                savedDoctors.Add(doc);
                List<TimeTable> timeTable = GetTable(savedDoctors, patient);
                FillTable(timeTable);
            }

        }

        private void bookView_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            if ((bookView.CurrentRow != null) && (bookView.CurrentRow.Cells[0].Value!=null))
                Order.Text = bookView.CurrentRow.Cells[0].Value.ToString();
            else
                Order.Text = "Прием не выбран";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (bookView.CurrentRow != null)
            {
                int id = Convert.ToInt32(bookView.CurrentRow.Cells[0].Value);
                TimeTable table = timetables.Where(x => x.ID == id).FirstOrDefault();
                table.PatientID = patient.Passport;
                table.Ordered = true;
                med.SaveChanges();
            }
            else
                MessageBox.Show("Прием не выбран");
        }

  

    }
}
